package com.it.security.details.member;

import com.it.api.UserFeignClient;
import com.it.comm.enums.PasswordEncoderTypeEnum;
import com.it.dto.UserAuthDTO;
import com.it.result.R;
import com.it.result.ResultCode;
import com.it.security.details.user.SysUserDetails;
import lombok.RequiredArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.security.authentication.AccountExpiredException;
//import org.springframework.security.authentication.DisabledException;
//import org.springframework.security.authentication.LockedException;
//import org.springframework.security.core.authority.SimpleGrantedAuthority;
//import org.springframework.security.core.userdetails.UserDetails;
//import org.springframework.security.core.userdetails.UserDetailsService;
//import org.springframework.security.core.userdetails.UsernameNotFoundException;
//import org.springframework.stereotype.Service;
//
//import java.util.ArrayList;
//import java.util.Collection;
//import java.util.List;
//import java.util.Objects;
//
//@Service("memberUserDetailsService")
//@Slf4j
//@RequiredArgsConstructor
//public class MemberUserDetailsServiceImpl implements UserDetailsService {
//
//    private final UserFeignClient userFeignClient;
//
//    @Override
//    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//        // 后面从管理端获取用户信息
//        R<UserAuthDTO> result = userFeignClient.getMemberUserByUsername(username);
//        SysUserDetails userDetails = null;
//        if (R.ok().getCode().equals(result.getCode())) {
//            UserAuthDTO user = result.getData();
//            if (null != user) {
//                userDetails = SysUserDetails.builder()
//                        .userId(user.getUserId())
//                        .username(user.getUsername())
//                        .authorities(handleRoles(user.getRoles()))
//                        .enabled(user.getStatus() == 1)
//                        .password(PasswordEncoderTypeEnum.BCRYPT.getPrefix() + user.getPassword())
//                        .build();
//            }
//        }
//        if (Objects.isNull(userDetails)) {
//            throw new UsernameNotFoundException(ResultCode.USER_NOT_EXIST.getMsg());
//        } else if (!userDetails.isEnabled()) {
//            throw new DisabledException("该账户已被禁用!");
//        } else if (!userDetails.isAccountNonLocked()) {
//            throw new LockedException("该账号已被锁定!");
//        } else if (!userDetails.isAccountNonExpired()) {
//            throw new AccountExpiredException("该账号已过期!");
//        }
//        return userDetails;
//    }
//
//    private Collection<SimpleGrantedAuthority> handleRoles(List<String> roles) {
//        Collection<SimpleGrantedAuthority> authorities = new ArrayList<>();
//        for (String role : roles) {
//            authorities.add(new SimpleGrantedAuthority(role));
//        }
//        return authorities;
//    }
//
//}
